<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/19
 * Time: 10:34
 */

namespace App\Admin\Extensions\From;


use Encore\Admin\Form;
use Encore\Admin\Form\Field;

class Tabs extends Field
{
    protected $view = 'admin.form.tabs';

    protected $_tabs = [];

    protected $_clickEventFunc = '';

    protected $fieldName = '';

    protected $_max_height = '100%';

    protected $_max_width = '100%';

    protected $_min_height = '100%';

    protected $_min_width = '100%';


    protected $width = [
        'label' => 5,
        'field' => 7
    ];

    public function __construct(string $column = '', array $arguments = [])
    {
        $this->checkViewFile();
        parent::__construct($column, $arguments);
    }

    public function setWidth($field = 8, $label = 4): Field
    {
        $label = 4;
        return parent::setWidth($field, $label); // TODO: Change the autogenerated stub
    }

    public function getViewElementClasses(): array
    {
        if ($this->horizontal) {
            return [
                'label'      => "col-sm-{$this->width['label']} {$this->getLabelClass()}",
                'field'      => "col-sm-{$this->width['field']}",
                'form-group' => $this->getGroupClass(true),
            ];
        }

        return ['label' => $this->getLabelClass(), 'field' => '', 'form-group' => ''];
    }

    protected function checkViewFile()
    {
        $dirPath = resource_path('views/admin/form');
        $filePath = $dirPath . '/tabs.blade.php';
        $originPath = __DIR__ . '/tabs.blade.php';
        if (!is_file($filePath) || filemtime($originPath) > filemtime($filePath)) {
            if (!is_dir($dirPath)) {
                mkdir($dirPath, 0777, true);
            }
            file_put_contents($filePath, file_get_contents($originPath));
        }
        return $this;
    }


    /**
     * @param array $arguments
     * @return string
     */
    public function formatLabel($arguments = []): string
    {
        if (isset($arguments[0]) && is_callable($arguments[0])) {
            $column = is_array($this->column) ? current($this->column) : $this->column;
            $this->_tabs[] = [
                'tabName' => $column,
                'closure' => $arguments[0],
            ];
            return ucfirst($column);
        } else {
            return parent::formatLabel($arguments); // TODO: Change the autogenerated stub
        }
    }

    /**
     * @param $fieldName
     * @return $this
     */
    public function setFieldName($fieldName)
    {
        $this->fieldName = $fieldName;
        return $this;
    }

    /**
     * @param $eventFun
     * @return $this
     */
    public function clickEvent($eventFun)
    {
        $this->_clickEventFunc = $eventFun;
        return $this;
    }

    /**
     * @param $title
     * @param \Closure $closure
     * @return $this
     */
    public function addTab($title, \Closure $closure)
    {
        $this->_tabs[] = [
            'tabName' => $title,
            'closure' => $closure,
        ];
        return $this;
    }

    /**
     * 设置最大宽高
     * @param $width
     * @param $height
     * @return $this
     */
    public function max($width, $height)
    {
        $this->_max_height = $height;
        $this->_max_width = $width;
        return $this;
    }

    /**
     * 设置最新宽高
     * @param $width
     * @param $height
     * @return $this
     */
    public function min($width, $height)
    {
        $this->_min_height = $height;
        $this->_min_width = $width;
        return $this;
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
     */
    public function render()
    {
        $tabs = [];
        $activeTab = '';
        $activeClass = '';
        foreach ($this->_tabs as $k => $tab) {
            $form = new Form($this->form->model());
            $closure = $tab['closure'];
            call_user_func($closure, $form);
            $content = $form->render();
            $reg = '/<form[^>]*>([\s\S]*)<div\s*class="box-footer"\s*>/';
            preg_match($reg, $content, $result);
            $tabs[] = [
                'tabName' => $tab['tabName'],
                'content' => $result[1] ?? '',
                'class'   => md5($tab['tabName']),
            ];
            if ($k == 0) {
                $activeTab = $tab['tabName'];
                $activeClass = md5($tab['tabName']);
            }
        }
//        dd($this->variables());
        $this->addVariables([
            'tabs'        => $tabs,
            'activeTab'   => $activeTab,
            'activeClass' => $activeClass,
            'eventFunc'   => $this->_clickEventFunc,
            'fieldName'   => $this->fieldName,
            'maxHeight'   => $this->_max_height,
            'maxWidth'    => $this->_max_width,
            'minHeight'   => $this->_min_height,
            'minWidth'    => $this->_min_width,
        ]);
        return parent::render(); // TODO: Change the autogenerated stub
    }


}